
#include "Shockwave.h"
#include "Utils/Vector2D.h"
#include "WorldObject.h"
#include "Utils/Timer.h"
#include "Graphics/TextureManager.h"

#include <GL/gl.h>
#include <cmath>


Shockwave::Shockwave(Vector2D position, float expand_speed, float duration, float width,
                     float col_r, float col_g, float col_b) :
    position(position), expand_speed(expand_speed), duration(duration),
    width(width), col_r(col_r), col_g(col_g), col_b(col_b),
    life(1.0f), life_decay(1.0f/duration), 
    radius(0.5f), inner_radius(0.0f) {
   
    tex_id = TextureManager::instance().loadTexture("data/Cloud.png");
     
} 

   
Shockwave::~Shockwave(){

}


void Shockwave::render(void){
    glPushMatrix();
    glTranslatef(position.x, position.y, 0.0f);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);   
 
    glEnable(GL_TEXTURE_2D);
    TextureManager::instance().bindTexture(tex_id);

    glBegin(GL_TRIANGLE_STRIP);

    for(unsigned i = 0; i <= 50; i++){
        float theta = M_PI*2.0f*i/50.0f;

        glColor4f(col_r, col_g, col_b, life);
        glTexCoord2f(0.5f, 0.0f);
        glVertex3f(sin(theta)*radius, cos(theta)*radius, 0.0f);

        glColor4f(col_r, col_g, col_b, life/2.0f);
        glTexCoord2f(0.5f, 1.0f);
        glVertex3f(sin(theta)*inner_radius, cos(theta)*inner_radius, 0.0f);
    }

    glEnd();

    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);

    glPopMatrix();
}

void Shockwave::update(void){
    life -= life_decay * Timer::secondsSinceLastFrame();
    radius += expand_speed * Timer::secondsSinceLastFrame();

    inner_radius = radius - width;
    if(inner_radius < 0.0f){ inner_radius = 0.0f; }
}


bool Shockwave::active(void){
    return life >= 0.0f;
}


bool Shockwave::resolveCollision(Ship *ship){ return false; }
bool Shockwave::resolveCollision(Weapon *weapon){ return false; }
bool Shockwave::resolveCollision(Map *map){ return false; }

